package com.jit.app.frament;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jit.app.AppMarketApplication;
import com.jit.app.AppSearchActivity;
import com.jit.app.R;
import com.jit.app.adapter.AdPromotionAdapter;
import com.jit.app.adapter.AppListAdapter;
import com.jit.app.listener.AppListItemClickListener;
import com.jit.app.model.AppInfoModel;
import com.jit.app.model.Constants;
import com.jit.app.util.JSONResult;

import java.util.List;

public class GameFragment extends Fragment {

    public static String tag = "game";

    View view;
    AdapterViewFlipper adViewFlipper;
    ListView appListView;
    Activity activity;
    Handler handler;
    AppListAdapter appListAdpater;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getActivity();

        view = inflater.inflate(R.layout.fragment_game,null);

        //创建Handler接收消息
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                JSONResult jsonResult = null;
                switch (msg.what){
                    case Constants.MessageWhat.GAMEAPPINFO_LOAD_SUCCESS:
                        jsonResult = (JSONResult) msg.obj;
                        if(jsonResult.getCode() == Constants.ResultCode.SUCCESS){
                            JSONArray jsonArray = (JSONArray) jsonResult.getData().get("gameOrSoftAppList");
                            List<JSONObject> list = jsonArray.toJavaList(JSONObject.class);
                            appListAdpater = new AppListAdapter(GameFragment.this.getActivity(),list,R.layout.applist_item);
                            appListView.setAdapter(appListAdpater);
                        }
                        break;
                        //加载轮播图
                    case Constants.MessageWhat.ADPROMOTION_LOAD_SUCCESS:
                        jsonResult = (JSONResult) msg.obj;
                        if(jsonResult.getCode() == Constants.ResultCode.SUCCESS){
                            JSONArray jsonArray = (JSONArray) jsonResult.getData().get("adPromotionList");
                            List<JSONObject> list = jsonArray.toJavaList(JSONObject.class);
                            AdPromotionAdapter adPromotionAdapter = new AdPromotionAdapter(GameFragment.this.getActivity(), list, R.layout.adpromotion_item);
                            adViewFlipper.setAdapter(adPromotionAdapter);
                            adViewFlipper.startFlipping();
                        }
                        break;
                    case Constants.MessageWhat.PROCESS_CHANGE:
                        Bundle bundle = msg.getData();
                        String pack = bundle.getString("pack");
                        double process = bundle.getDouble("process");
                        int first = appListView.getFirstVisiblePosition();
                        int last = appListView.getLastVisiblePosition();

                        for(int i=0;i<last-first+1;i++){
                            View item = appListView.getChildAt(i);
                            JSONObject appInfo = (JSONObject) appListView.getItemAtPosition(i+first);
                            if(pack.equals(appInfo.getString("apkName"))){
                                ProgressBar pb = item.findViewById(R.id.pb_download);
                                pb.setProgress((int)(process*100));
                                break;
                            }
                        }
                        break;
                    case Constants.MessageWhat.DOWNLOAD_COMPLETE:
                        Bundle bundle2 = msg.getData();
                        String pack2 = bundle2.getString("pack");
                        String apkPath = bundle2.getString("apkPath");
                        int first2 = appListView.getFirstVisiblePosition();
                        int last2 = appListView.getLastVisiblePosition();

                        for(int i=0;i<last2-first2+1;i++){
                            View item = appListView.getChildAt(i);
                            JSONObject appInfo = (JSONObject) appListView.getItemAtPosition(i+first2);
                            if(pack2.equals(appInfo.getString("apkName"))){
                                //把进度条隐藏，把安装按钮显示出来
                                ProgressBar pb = item.findViewById(R.id.pb_download);
                                pb.setVisibility(View.INVISIBLE);
                                TextView install = item.findViewById(R.id.btn_install);
                                install.setVisibility(View.VISIBLE);
                                install.setTag(bundle2);
                                break;
                            }
                        }
                        break;
                }

                return true;
            }
        });

        AppMarketApplication.downloadBinder.addHandler(handler);

        initView();
        initData();

        return view;
    }

    TextView searchTextView;

    public void initView(){

        searchTextView = view.findViewById(R.id.tv_search);
        searchTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, AppSearchActivity.class);
                activity.startActivity(intent);
            }
        });

        adViewFlipper = view.findViewById(R.id.vf_ad);
        appListView = view.findViewById(R.id.lv_gameapplist);

        appListView.setOnItemClickListener(new AppListItemClickListener(activity));
    }


    AppInfoModel appInfoModel = new AppInfoModel();
    public void initData(){

        //加载appInfo信息
        appInfoModel.loadGameAppInfoList(handler);

        //加载轮播图信息
        appInfoModel.loadAdPromotionList(handler);

    }

    @Override
    public void onDestroyView() {
        AppMarketApplication.downloadBinder.removeHandler(handler);
        super.onDestroyView();
    }

}
